﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using Timer = System.Timers.Timer;
using AllInOne.PrintUtils;
using System.Threading;

namespace AllInOne.MainPages
{
    /// <summary>
    /// PrintProcessPage.xaml 的交互逻辑
    /// </summary>
    public partial class PrintProcessPage : Page
    {
        private MainWindow parent;

        public Timer processBarTimer = null;

        private PrintUtil printUtil = new PrintUtil();


       

        public PrintProcessPage()
        {
            InitializeComponent();
        }

        public PrintProcessPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }


        public void InitPage() {
            this.InitProcessBarTimer();
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            this.percent = 0;
            this.processWidth = 0;
        }

        public void ResetProcess() {
            this.percent = 0;
            this.processWidth = 0;
            this.processBarTimer.Enabled = true;

        }


        private int percent = 0;
        private int index = 0;
        private int processWidth = 0;
        private int second = 120;
         
        public void InitProcessBarTimer()
        {
            if (this.processBarTimer == null)
            {
                this.processBarTimer = new Timer();
                this.processBarTimer.Interval = 30;
                this.processBarTimer.Elapsed += new ElapsedEventHandler(delegate (object sender, ElapsedEventArgs e) {
                    if (processWidth < percent * 5)
                    {
                        processWidth +=5;
                    }
                    this.index--;
                    double left = -index;
                    double right = 500 - processWidth;
                    this.Dispatcher.Invoke(new Action(delegate ()
                    {
                        this.imgProcessBarBg.Margin = new Thickness(left, 0, right, 0);

                    }));
                    if (this.index <=0 )
                    {
                        this.index = 25;
                    }
                });
            }
        }


        public void setProcess(int percent, int second)
        {
            if (percent>=100)
            {
                this.parent.SendMsgToController(new byte[] { 0xaa, 0xb3, 0x01, 0x01, 0x00, 0x00, 0x00, 0xdd });  //打印业务受理结束
                this.parent.PlaySound(Properties.Resources.male_build_complete, Properties.Resources.female_build_complete);
                new Thread(new ThreadStart(delegate ()
                {
                    Thread.Sleep(2000);
                    this.parent.ResetIndex();
                }))
                { IsBackground = true }.Start();
                

            }
            this.Dispatcher.Invoke(new Action(delegate(){
                this.percent = percent;
                this.second = second;
                this.txtPercent.Text = percent + "%";
                this.txtSecond.Text = second + "";
            }));
        }
        

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (this.percent >= 100)
            {
                this.processBarTimer.Enabled = false;
                this.parent.ResetIndex();
            }
            else {
                this.parent.Alert("提示", "证件制作中，请勿离开。");
            }
        }


        private string zxbh;
        private string lsh;
        public bool loginFlag = false;
        //private bool printDrivingLicenseFlag = false;
        //private bool printLnmjFlag = false;
        //private byte[] finishCommand;


        public bool StartPrintDrivingLicense(string zxbh, string lsh)
        {
            this.zxbh = zxbh;
            this.lsh = lsh;
            //this.finishCommand = finishCommand;

            bool printFlag = false;
            this.Dispatcher.Invoke(new Action(delegate ()
            {
                printFlag = this.printUtil.PrintPaper(this.zxbh, this.lsh);
            }));
            return printFlag;
            //this.printDrivingLicenseFlag = true;
        }

        public bool StartPrintLnmj(string lsh)
        {
            this.lsh = lsh;
            bool printFlag = false;
            this.Dispatcher.Invoke(new Action(delegate ()
            {
                printFlag = this.printUtil.PrintLogo(this.lsh);
            }));
            //this.printLnmjFlag = true;
            return printFlag;
        }

        public bool Login(string verifyCode, string userName, string password)
        {



            SimpleConsole.WriteInfo(string.Format("验证码 {0},username {1},pwd {2}", verifyCode, userName, password), this);
            this.printUtil.Login(verifyCode, userName, password);

            Thread.Sleep(1500);
            var checkLoginIndex = 0;
            while (checkLoginIndex < 30)
            {
                checkLoginIndex++;
                loginFlag = this.printUtil.CheckLogin();
                if (loginFlag)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(200);
                }
            }

            return loginFlag;
        }

        //public void InitPrintTask()
        //{

        //    new Thread(new ThreadStart(delegate ()
        //    {
        //        while (true)
        //        {
        //            Thread.Sleep(200);
        //            if (printDrivingLicenseFlag)
        //            {
        //                printDrivingLicenseFlag = false;
        //                this.Dispatcher.Invoke(new Action(delegate ()
        //                {
        //                    this.printUtil.PrintPaper(this.zxbh, this.lsh);
        //                }));
        //            }
        //            if (printLnmjFlag)
        //            {
        //                printLnmjFlag = false;
        //                this.Dispatcher.Invoke(new Action(delegate ()
        //                {
        //                    this.printUtil.PrintLogo( this.lsh);
        //                }));
        //            }
        //        }
        //    }))
        //    {
        //        IsBackground = true
        //    }.Start();
        //}

        public System.Drawing.Image GetVerifyCode(string ipAddr)
        {
            System.Drawing.Image verifyImage = this.printUtil.GetLoginVerifyCode(ipAddr);
            return verifyImage;
        }


        private System.Timers.Timer checkLoginTimer = null;

        public void InitLoginTimer()
        {
            checkLoginTimer = new System.Timers.Timer();
            checkLoginTimer.Interval = 30000;
            checkLoginTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate (object sender, System.Timers.ElapsedEventArgs e)
            {

                if (!loginFlag)
                {
                    return;
                }
                if (this.parent.appStep == AppStep.INDEX)
                {
                    loginFlag = this.printUtil.CheckLogin();
                }
            });
            checkLoginTimer.Enabled = true;
        }

    }
}
